package design_patterns_and_best_practices_in_java;

import java.util.stream.Collectors;

interface PrintText {
    void print(String text);
}

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @create 2019-11-14 下午4:10
 */
public class Chapter4Decorator {
    public static void main(String[] args) {
        final String text = "text";
        final PrintText object = new PrintAsciiText();
        final PrintText printer = new PrintTextHexDecorator(object);
        object.print(text);
        printer.print(text);
    }
}

class PrintAsciiText implements PrintText {

    @Override
    public void print(String text) {
        System.out.println("Print ASCII: " + text);
    }
}

class PrintTextHexDecorator implements PrintText {

    private PrintText inner;

    public PrintTextHexDecorator(PrintText inner) {
        this.inner = inner;
    }

    @Override
    public void print(String text) {
        String hex = text.chars().boxed().map(x -> "0x" + Integer.toHexString(x))
                .collect(Collectors.joining(" "));
        inner.print(text + " -> HEX: " + hex);
    }
}
